package com.kx.yooli.kx.wechat.base.mdc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author john_sqwen-childe.wen
 * date 2024/4/8
 * PROJECT_NAME wechat
 * description create TransferMDC by john_sqwen
 * and  value is
 */
public class TransferMDC {
    private final Logger log = LoggerFactory.getLogger(TransferMDC.class);
    public TransferMDC() {
    }

    public static String getValue(String key) {
        return MDC.get(key);
    }

    public static void setValue(String key, String value) {
        MDC.put(key, value);
    }

    public static void setValues(Map<String, String> values) {
        if (!CollectionUtils.isEmpty(values)) {

            for (Map.Entry<String, String> stringStringEntry : values.entrySet()) {
                Map.Entry entry = (Map.Entry) stringStringEntry;
                MDC.put((String) entry.getKey(), (String) entry.getValue());
            }

        }
    }

    public static Map<String, String> getAllValues() {
        Map<String, String> values = MDC.getCopyOfContextMap();
        return (Map)(CollectionUtils.isEmpty(values) ? new HashMap(3) : values);
    }

    public static void setContextMap(Map<String, String> values) {
        MDC.setContextMap(values);
    }

    public static void clear() {
        MDC.clear();
    }
}
